package com.qnw.CardGroupManagement;

import android.app.*;
import android.content.*;
import android.os.*;
import android.text.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import java.io.*;
import java.util.*;
import com.ygo.feihua.R;
import com.ygo.feihua.Gongju;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.ygo.feihua.view.*;
import com.ygo.feihua.util.*;

public class I5 extends AppCompatActivity implements OnClickListener
{
	private EditText ed_lua;
	private EditText ed_km;
	public String zypath;
	public String km;
	public String lua;
	private Gongju gj;
	private DialogUtils du;
	private Toolbar tool;
	private ImageView i5_ss;
	
	String back_String="";
	
	@Override
	public void onClick(View p1)
	{
		switch (p1.getId()){
			case R.id.i5_ss:
				DialogUtils.CardList dc= du.dialogCxCards();
				dc.setOnCardItemClickListener(new DialogUtils.OnCardItemClickListener(){

						@Override
						public void onClick(Card card)
						{
							du.dis();
							ed_km.setText(card.getId());
							String jb=gj.getjb(ed_km.getText().toString().trim());
							if(!jb.equals("")){
								ed_lua.setText(jb);
							}else{
								gj.show("该卡片脚本不存在");
							}
							// TODO: Implement this method
						}
					});
				break;
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.i5_layout);

		ed_lua = (EditText)findViewById(R.id.textEditorView);
		ed_km = (EditText)findViewById(R.id.i5_ed_km);
		tool=(Toolbar) findViewById(R.id.toolbar);
		i5_ss=(ImageView) findViewById(R.id.i5_ss);
		
		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		zypath=SharedPreferenceUtil.getYgopath();
		gj.cshToolbar(tool,"lua编辑");	

		i5_ss.setOnClickListener(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.i5, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.m_luabc:
				{
					km = ed_km.getText().toString().trim();
					lua = ed_lua.getText().toString();
					if (TextUtils.isEmpty(km) || TextUtils.isEmpty(lua))
					{
						gj.show( "不能为空");
						return true; 
					}
					File f=new File(zypath + "script/c" + km + ".lua");
					if (f.exists())
					{
						
						View[] v=du.dialogt(false,"","lua已存在,是否覆盖？");
						Button b1,b2;
						b1=(Button) v[0];
						b2=(Button) v[1];
						b1.setText("取消");
						b2.setText("覆盖");
						b1.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View p1)
								{
									du.dis();
									// TODO: Implement this method
								}
							});
						b2.setOnClickListener(new OnClickListener(){

								@Override
								public void onClick(View p1)
								{
									try
									{
										BufferedWriter br=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(zypath + "script/" + km + ".lua"),"utf-8"));
										br.write(lua);
										br.close();
										gj.show( "已覆盖");
									}
									catch (IOException e)
									{
										gj.show("覆盖失败,原因为"+e);
									}
									du.dis();

									// TODO: Implement this method
								}
							});
					}else{
						try
						{
							BufferedWriter br=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f),"utf-8"));
							br.write(lua);
							br.close();
						}
						catch (IOException e)
						{}
						gj.show( "已保存到" + f.toString());
					}
					break;
				}
			case R.id.m_luadq:
				{
					km = ed_km.getText().toString().trim();
					if (TextUtils.isEmpty(km))
					{
						gj.show( "卡密不能为空");
					}else{
						String jb=gj.getjb(km);
						if(!jb.equals("")){
							ed_lua.setText(jb);
						}else{
							gj.show("该卡片脚本不存在");
						}
					}
					break;
				}
		}
		return true;
	}
}
